//
//  BUBaseRequestManager.m
//  BUBU3D
//
//  Created by lichentao on 14-7-22.
//  Copyright (c) 2014年 chentaoApp. All rights reserved.
//

#import "BUBaseRequestManager.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"
#import "Constants.h"

#define STATUS @"error_code"
#define UPLOAD_NAME @"upfile"

@implementation BUBaseRequestManager

// 单例
//+ (BUBaseRequestManager *)sharedRequest{
//    static  BUBaseRequestManager *_obj = nil;
//    static dispatch_once_t token;
//    dispatch_once(&token,^{
//        _obj = [[BUBaseRequestManager alloc] init];
//        
//    });
//    return _obj;
//}
/**
 *  POST - FALSE | TRUE
 *  @paramer:path
 *  @paramer:parameter参数
 *  @return
 */
// 返回成功或者失败
- (void)requestPostWithParameterForSuccessOrFailBlock:(NSString *)path SetParamer:(NSDictionary *)parameter success:(SuccessBlock)success
                                              failure:(void (^)(NSString *errorMsg))failure{
 
    AFHTTPRequestOperationManager *manager1 =[AFHTTPRequestOperationManager manager];
    manager1.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [manager1 POST:path parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSDictionary *responseDictionary = (NSDictionary *)responseObject;
            // isuccess-1 数据加载成功
            NSNumber *issuccess = [responseDictionary objectForKey:STATUS];
            // 数据返回成功
            if (issuccess && [issuccess boolValue]) {
                
                dispatch_async(dispatch_get_main_queue(), ^(){
                    success([issuccess boolValue]);
                });
                
            } else {
                [self reCallBackErrorMsgSetResponseDictionary:responseDictionary failure:^(NSString *errorMsg){
                    failure(errorMsg);
                }];
            }
        } failure:^(AFHTTPRequestOperation *operation,NSError *error){
            [MBProgressHUD hideAllHUDsForView:[AppDelegate shareApplication].window animated:YES];
            failure(error.description);
        }];
    });
}

/**
 *  GET
 *  @paramer:path
 *  @paramer:parameter参数
 *  @return
 */
- (void)requestGetWithParameterWithBlockDic:(NSString *)path SetParamer:(NSDictionary *)parameter
                                    success:(void (^)(NSDictionary  *responseDictionary))success
                                    failure:(void (^)(NSString *errorMsg))failure{
    
    
    NSString *finalPath = [PATH_BASE stringByAppendingString:path];
   
    AFHTTPRequestOperationManager *manager1 =[AFHTTPRequestOperationManager manager];
    manager1.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    [MBProgressHUD showHUDAddedTo:[AppDelegate shareApplication].window animated:YES];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [manager1 GET:finalPath parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [MBProgressHUD hideAllHUDsForView:[AppDelegate shareApplication].window animated:YES];

            NSDictionary *responseDictionary = (NSDictionary *)responseObject;
            NSLog(@"%@",responseDictionary);
            // isuccess-1 数据加载成功
            NSNumber *issuccess = [responseDictionary objectForKey:STATUS];
            // 数据返回成功
            if (issuccess && [issuccess boolValue] == 0) {
                dispatch_async(dispatch_get_main_queue(), ^(){
                    success(responseDictionary);
                });
            } else {
                [self reCallBackErrorMsgSetResponseDictionary:responseDictionary failure:^(NSString *errorMsg){
                    failure(errorMsg);
                }];
            }
        } failure:^(AFHTTPRequestOperation *operation,NSError *error){
            dispatch_async(dispatch_get_main_queue(), ^(){
                [MBProgressHUD hideAllHUDsForView:[AppDelegate shareApplication].window animated:YES];
                failure(error.description);
            });
        }];
    });
  }

/**
 *  POST
 *  @paramer:path
 *  @paramer:parameter参数
 *  @return
 */
- (void)requestPostWithParameterWithBlockDic:(NSString *)path SetParamer:(NSDictionary *)parameter
                                     success:(void (^)(NSDictionary  *responseDictionary))success
                                     failure:(void (^)(NSString *errorMsg))failure{
     NSString *finalPath ;
    if (path && path.length > 0) {
       finalPath = [PATH_BASE stringByAppendingString:path];
    }else{
        return;
    }
    

    AFHTTPRequestOperationManager *manager1 =[AFHTTPRequestOperationManager manager];
    manager1.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    [MBProgressHUD showHUDAddedTo:[AppDelegate shareApplication].window  animated:YES];
    
    [manager1 POST:finalPath parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideAllHUDsForView:[AppDelegate shareApplication].window animated:YES];

        NSDictionary *responseDictionary = (NSDictionary *)responseObject;
        // isuccess-1 数据加载成功
        NSNumber *error_code = [responseDictionary objectForKey:STATUS];
        // 数据返回成功
        if (error_code.intValue == 0) {
            dispatch_async(dispatch_get_main_queue(), ^(){
                success(responseDictionary);
            });
        } else {
            [self reCallBackErrorMsgSetResponseDictionary:responseDictionary failure:^(NSString *errorMsg){
                dispatch_async(dispatch_get_main_queue(), ^(){
                    failure(errorMsg);
                });
            }];
        }
    } failure:^(AFHTTPRequestOperation *operation,NSError *error){
        dispatch_async(dispatch_get_main_queue(), ^(){
            [MBProgressHUD hideAllHUDsForView:[AppDelegate shareApplication].window animated:YES];
            failure(error.description);

        });
    }];
}


// 上传图片 post
- (void)requestLoadImgWihtPathAndImgPath:(NSString *)path setParamer:(NSDictionary *)paramer SetImgPath:(NSString *)imgPath
                                     success:(void (^)(NSDictionary  *responseDictionary))success
                                     failure:(void (^)(NSString *errorMsg))failure{
    
    
     NSString *finalPath = [PATH_BASE stringByAppendingString:path];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json",@"multipart/form-data", @"text/json", @"text/javascript",@"text/html", nil]];

    NSURL *filePath = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@",imgPath]];
   
    [manager POST:finalPath parameters:paramer constructingBodyWithBlock:^(id formData) {
        [formData appendPartWithFileURL:filePath name:UPLOAD_NAME error:nil];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {

        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
    }];

}

- (void)reCallBackErrorMsgSetResponseDictionary:(NSDictionary *)responseDictionary failure:(void (^)(NSString *errorMsg))failure{
    NSString *msg = @"网络出错了";
    if ([responseDictionary objectForKey:@"message"]) {
        msg = [responseDictionary objectForKey:@"message"];
    }
    failure(msg);
}

@end
